// pages/table/index.js
const app = getApp()
import http from '../../utils/request.js';
import alert from '../../utils/alert.js';
import { $wuxDialog } from '../../wux/index'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    table:[],
    order:[],
    self:false,
    buttons: [{
      type: 'assertive',
      block: true,
      text: '扫码点餐',
    }
    ],
    resultIcon: {
      type: 'info',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.mode == "scan" && options.table_id !== undefined) {
      this.getOrderByTable(options.table_id);
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  getOrderByTable: function (table) {
    http.post(
      app.config.service.getOrderByTable,
      { app_id: app.globalData.app_id, table_id: table }
    ).then(data => {
      if (!data.order){
        wx.redirectTo({
          url: '../goods/index?table_id=' + table,
        })
      }else{
        var userInfo=wx.getStorageSync('userInfo');
        if (data.order.uid == userInfo.uid){
          var self=true;
        }else{
          var self =false
        }
        this.setData({
          order:data.order,
          table:data.table,
          self: self
        });
          wx.setNavigationBarTitle({
            title: data.table.name
          });
      }
    }, err => {
      console.log(err)
    });
  },

  goOrder:function(){
    wx.navigateTo({
      url: '../order/info/index?order_no='+this.data.order.order_no,
    })
  },

  scanTableQr: function (e) {
    var that = this;
    var scanOptions = {
      onlyFromCamera: true,
      scanType: 'qrCode',
      success(res) {
        console.log(res)
        if (res.scanType == "WX_CODE") {
          console.log(res.path)
          var table_id = http.urlparams(res.path, 'table_id')
          var mode = http.urlparams(res.path, 'mode')
          that.getOrderByTable(table_id)
        }
      },
      fail(error) {
        console.log(error.errMsg)
      }
    };
    wx.scanCode(scanOptions)
  },
  pleaseOrder: function(e){
    var that = this;
    console.log(that.data.self);
    if (that.data.self){
      wx.navigateTo({
        url: '../goods/index?table_id=' + that.data.table.id + '&order_no=' + that.data.order.order_no
      })
    }else{
      $wuxDialog().prompt({
        resetOnClose: true,
        title: '下单密码',
        content: '密码为6位数字',
        fieldtype: 'number',
        password: !0,
        defaultText: '',
        placeholder: '请输入下单密码',
        maxlength: 6,
        onConfirm(e, response) {
          if (parseInt(response) === parseInt(that.data.order.op_pwd)){
            wx.navigateTo({
              url: '../goods/index?table_id=' + that.data.table.id + '&order_no=' + that.data.order.order_no
            })
          }else{
            alert.alert('操作密码错误')
          }
        },
      })
    }

  },
  setOrderPwd:function(e){
    var that = this;
    $wuxDialog().prompt({
      resetOnClose: true,
      title: '设置密码',
      content: '密码为6位数字,别人知道此密码才可下单',
      fieldtype: 'number',
      password: !0,
      defaultText: '',
      placeholder: '请输入下单密码',
      maxlength: 6,
      onConfirm(e, response) {
        if (parseInt(response) === parseInt(that.data.order.op_pwd)) {
          alert.alert('与原密码一样')
        } else {
          http.post(app.config.service.setOrderPwd, {
            order_no: that.data.order.order_no,
            op_pwd: response
          }).then(data => {
            this.setData({
              is_row_line: data.is_row_line,
              typeLists: data.lists,
              max_line_fail_num: data.max_line_fail_num
            })
          }, err => {
            alert.alert(err)
          });
        }
      },
    })
  },
  callWaiter:function(){
    http.post(app.config.service.callWaiter, {
      table_id: this.data.table.id
    }).then(data => {
      console.log(data);
    }, err => {
      alert.alert(err)
    });
  }
})